package com.rub.prajna.av.view

import com.rub.prajna.av.model.FileVo
import com.rub.prajna.core.utils.FileKit
import javafx.event.EventHandler
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.control.Alert
import javafx.scene.control.Button
import javafx.scene.control.ButtonType
import javafx.scene.control.TextField
import javafx.stage.DirectoryChooser
import javafx.stage.FileChooser
import javafx.stage.Stage
import java.awt.Desktop
import java.io.File
import java.util.stream.Collectors

/**
 * 基础公共View
 * .mp4,.flv,.wmv,.avi,.mov,.dat,.asf,.rm,.rmvb,.ram,.mpg,.mpeg,.3gp,.m4v,.dvix,.dv,.mkv,.vob,.qt,.cpk,.fli,.flc,.mod,.ts,.webm,.m2ts
 */
abstract class BaseView : Initializable {
    @FXML
    lateinit var inputFile: TextField

    @FXML
    lateinit var outputFile: TextField

    @FXML
    lateinit var btnBrowser: Button

    @FXML
    lateinit var btnPreView: Button


    @FXML
    private fun onActionBrowser() {

        btnBrowser?.onAction = EventHandler {
            val fileChooser = FileChooser()
            val extFilter = FileChooser.ExtensionFilter(
                "视频文件",
                initVideoType()
            )
            fileChooser.extensionFilters.add(extFilter)
            val file = fileChooser.showOpenDialog(Stage())
            inputFile?.text = file?.absolutePath
        }
    }

    @FXML
    private fun onActionBrowsers() {

        btnBrowser?.onAction = EventHandler {
            var dc = DirectoryChooser()
            dc.title = "请选择文件夹"
            var file = dc.showDialog(Stage())
            inputFile?.text = file?.absolutePath

        }
    }

    @FXML
    private fun onActionPreView() {
        val filePath = outputFile?.text
        if (filePath == null || filePath == "") {
            Alert(Alert.AlertType.ERROR, "文件未生成不能预览", ButtonType.OK).show()
            return
        }
        Desktop.getDesktop().open(File(filePath))
    }

    /**
     *
     */
    fun initImageType(): List<String>? {
        val arrays: MutableList<String> = ArrayList()
        arrays.apply {
            add("jpeg")
            add("jpg")
            add("png")
        }
        return arrays
    }

    /**
     *
     */
    private fun initVideoType(): List<String> {
        val arrays: MutableList<String> = ArrayList()
        arrays.apply {
            add("*.mp4")
            add("*.wmv")
            add("*.avi")
            add("*.flv")
            add("*.mpeg")
            add("*.mpg")
        }
        return arrays
    }


    /**
     * 时:分:秒 --> 01:10:01
     */
    fun timeLists(time: Int): List<String>? {
        val priorities: MutableList<String> = java.util.ArrayList()
        for (i in 0 until time) {
            if (i < 10) {
                priorities.add("0$i")
            } else {
                priorities.add(i.toString())
            }
        }
        return priorities
    }


    /**
     * 通过路径递归所有视频文件
     * 去掉重复的视频数据
     */
    fun listVideo(filePath: String): MutableList<FileVo> {
        var fileTypes = ArrayList<FileVo>()

        var listFiles = FileKit().listFiles(filePath, ArrayList())

        listFiles?.forEach{
            var file = File(it)
            var fileName = file.name
            val fileType = fileName.substring(fileName.lastIndexOf(".") + 1)
            fileTypes.add(FileVo(it as String, "*.$fileType",fileName))
        }
        //通过视频类型进行分组去重
        fileTypes.distinctBy { it.fileType }

        return fileTypes.stream().filter { return@filter initVideoType().contains(it.fileType) }
            .collect(Collectors.toList())
    }
}
